package com.basic.business.unsalableBacklog.service.impl;

import com.basic.business.dingTalk.utils.DingTalkUtils;
import com.basic.business.kanban.vo.UnsalableCollectRespVo;
import com.basic.business.kanban.vo.UnsalableReplayPageReqVo;
import com.basic.business.kanban.vo.UnsalableReplayRespVo;
import com.basic.business.labelMapping.domain.TLabelMapping;
import com.basic.business.labelMapping.mapper.TLabelMappingMapper;
import com.basic.business.pic.mapper.TGoodsPicMapper;
import com.basic.business.store.domain.TStoreMgmt;
import com.basic.business.store.mapper.TStoreMgmtMapper;
import com.basic.business.unsalableBacklog.domain.TUnsalableBacklog;
import com.basic.business.unsalableBacklog.mapper.TUnsalableBacklogMapper;
import com.basic.business.unsalableBacklog.service.ITUnsalableBacklogService;
import com.basic.common.annotation.DataScope;
import com.basic.common.core.domain.entity.SysUser;
import com.basic.common.core.service.impl.BaseServiceImpl;
import com.basic.common.utils.DateUtils;
import com.basic.common.utils.SecurityUtils;
import com.basic.common.utils.UserUtils;
import com.basic.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 滞销代办事项Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TUnsalableBacklogServiceImpl extends BaseServiceImpl<TUnsalableBacklogMapper, TUnsalableBacklog> implements ITUnsalableBacklogService {

    @Resource
    private TUnsalableBacklogMapper tUnsalableBacklogMapper;

    @Resource
    private TStoreMgmtMapper tStoreMgmtMapper;

    @Resource
    private TLabelMappingMapper tLabelMappingMapper;

    @Resource
    private TGoodsPicMapper tGoodsPicMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public int insertBacklog(TUnsalableBacklog tUnsalableBacklog) {

        //负责人
        TLabelMapping leader = tLabelMappingMapper.getUserByLabel(tUnsalableBacklog.getGoodsLabel());

        if (leader != null) {
            tUnsalableBacklog.setLeaderId(leader.getMainId());
            tUnsalableBacklog.setLeaderName(leader.getMainLabel());
        } else {
            //店铺负责人
            SysUser storeLeader = tStoreMgmtMapper.getUserByStoreId(tUnsalableBacklog.getStoreId());
            if (storeLeader != null) {
                tUnsalableBacklog.setLeaderId(storeLeader.getUserId());
                tUnsalableBacklog.setLeaderName(storeLeader.getNickName());
            }
        }

        tUnsalableBacklog.setCreateTime(DateUtils.getNowDate());
        tUnsalableBacklog.setCreateBy(SecurityUtils.getUsername());
        int insert = tUnsalableBacklogMapper.insert(tUnsalableBacklog);

        if (insert > 0) {
            //异步发送钉钉消息
            CompletableFuture.runAsync(() -> sendDingTalkMessage(tUnsalableBacklog));
        }

        return insert;
    }

    /**
     * 发送待办事项钉钉消息
     *
     * @param log
     */
    public void sendDingTalkMessage(TUnsalableBacklog log) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        TStoreMgmt tStoreMgmt = tStoreMgmtMapper.get(log.getStoreId());

        String content = String.format("\uD83D\uDCE5 [温馨提示]滞销待优化通知，请相关人员查看\n" +
                "店铺名称：%s \n" +
                "商品ID：%s \n" +
                "商品标签：%s \n" +
                "商品判定类型：%s \n" +
                "行动点：%s \n" +
                "负责人：%s \n" +
                "生成时间：%s", tStoreMgmt.getStoreName(), log.getGoodsId(), log.getGoodsLabel(), log.getJudgeType(), log.getActionPoint(), log.getLeaderName(), sdf.format(log.getCreateTime()));

        // 通过钉钉发送消息
        if (sysDeptMapper.selectDeptIdsByDeptId(110L).contains(log.getDeptId())) {
            //3C事业部通知
            DingTalkUtils.send(content, "DUH", UserUtils.getMobileByUserId(log.getLeaderId().toString()));
        } else if (sysDeptMapper.selectDeptIdsByDeptId(111L).contains(log.getDeptId())) {
            //海报事业部通知
            DingTalkUtils.send(content, "DBH", UserUtils.getMobileByUserId(log.getLeaderId().toString()));
        }

    }

    @Override
    @DataScope(deptAlias = "t")
    public List<UnsalableReplayRespVo> getOptimizeList(UnsalableReplayPageReqVo unsalableReplayPageReqVo) {
        List<UnsalableReplayRespVo> list = tUnsalableBacklogMapper.getOptimizeList(unsalableReplayPageReqVo);
        list.forEach(item -> {
            item.setImgUrl(tGoodsPicMapper.getPicUrl(String.valueOf(item.getGoodsId())));
        });
        return list;
    }

    @Override
    @DataScope(deptAlias = "t")
    public UnsalableCollectRespVo getUnsalableCollectData(UnsalableReplayPageReqVo unsalableReplayPageReqVo) {
        return tUnsalableBacklogMapper.getUnsalableCollectData(unsalableReplayPageReqVo);
    }

    @Override
    public int getBackLogCountByGoodsId(String goodsId) {
        return tUnsalableBacklogMapper.getBackLogCountByGoodsId(goodsId);
    }

    @Override
    public void handleTimeoutBacklog() {
        tUnsalableBacklogMapper.getTimeoutBacklog().forEach(this::sendTimeOutDingTalkMessage);
    }

    /**
     * 待办事项超时发送钉钉消息
     *
     * @param log
     */
    public void sendTimeOutDingTalkMessage(TUnsalableBacklog log) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        TStoreMgmt tStoreMgmt = tStoreMgmtMapper.get(log.getStoreId());

        String content = String.format("⏲️ [超时提示]滞销待优化超时通知，请相关人员及时处理\n" +
                "店铺名称：%s \n" +
                "商品ID：%s \n" +
                "商品标签：%s \n" +
                "商品判定类型：%s \n" +
                "行动点：%s \n" +
                "负责人：%s \n" +
                "生成时间：%s", tStoreMgmt.getStoreName(), log.getGoodsId(), log.getGoodsLabel(), log.getJudgeType(), log.getActionPoint(), log.getLeaderName(), sdf.format(log.getCreateTime()));

        // 通过钉钉发送消息
        if (sysDeptMapper.selectDeptIdsByDeptId(110L).contains(log.getDeptId())) {
            //3C事业部通知
            DingTalkUtils.send(content, "DUH", UserUtils.getMobileByUserId(log.getLeaderId().toString()));
        } else if (sysDeptMapper.selectDeptIdsByDeptId(111L).contains(log.getDeptId())) {
            //海报事业部通知
            DingTalkUtils.send(content, "DBH", UserUtils.getMobileByUserId(log.getLeaderId().toString()));
        }

    }
}
